package cn.edu.zjicm.wordsnet_d.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.util.BitmapUtil;

import com.umeng.analytics.MobclickAgent;

public class LockWallpaperSelectActivity extends WordBaseActivity {

	public static void startActivity(Context context) {
		Intent i = new Intent(context, LockWallpaperSelectActivity.class);
		context.startActivity(i);
	}

	static final int TAKE_AVATAR_GALLERY_REQUEST = 2;
	static final int TAKE_DEFAULT_PICTURE = 3;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (requestCode) {
		case TAKE_AVATAR_GALLERY_REQUEST:

			if (resultCode == Activity.RESULT_CANCELED) {
				// Avatar gallery request mode was canceled.
			} else if (resultCode == Activity.RESULT_OK) {

				// Get image picked
				Uri photoUri = data.getData();
				if (photoUri != null) {
					try {
						Bitmap galleryPic = Media.getBitmap(getContentResolver(), photoUri);
						// Log.e("bitmap","1");
						Bitmap scaledGalleryPic = BitmapUtil.CropForWindow(galleryPic, this);
						// Log.e("bitmap","2");
						if (scaledGalleryPic == null) {
							Toast.makeText(LockWallpaperSelectActivity.this, "图像处理失败，请选择大小合适的图像", Toast.LENGTH_SHORT).show();
							return;
						}

						BitmapUtil.saveAvatar(scaledGalleryPic, this);
						// Log.e("bitmap","3");
						String wallpaper = AppPreference.getLockScreenWallpaper(this);
						Uri imageUri = Uri.parse(wallpaper);
						mWallpaper.setImageURI(null);
						mWallpaper.setImageURI(imageUri);
						// Log.e("bitmap","4");
					} catch (Exception e) {
						Toast.makeText(LockWallpaperSelectActivity.this, "图像处理失败，请选择大小合适的图像", Toast.LENGTH_SHORT).show();
					} catch (OutOfMemoryError e) {
						Toast.makeText(LockWallpaperSelectActivity.this, "图像处理失败，请选择大小合适的图像", Toast.LENGTH_SHORT).show();
					}
				}
			}
			break;
		case TAKE_DEFAULT_PICTURE:
			if (resultCode == Activity.RESULT_OK) {
				mWallpaper.setImageResource(AppPreference.getDefaultWallpaperImg(LockWallpaperSelectActivity.this));
			}
			break;
		}
	}

	private ImageView mWallpaper;
	private TextView mDefaultTextView, mMyTextView, mSystemTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitleBarStyle(WordBaseActivity.TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("锁屏壁纸更换");
		setContentView(R.layout.activity_lock_wallpaper_select);
		findViews();
		initViews();
	}

	private void findViews() {
		mWallpaper = (ImageView) findViewById(R.id.wallpaper);
		mDefaultTextView = (TextView) findViewById(R.id.default_paper);
		mMyTextView = (TextView) findViewById(R.id.my_paper);
		mSystemTextView = (TextView) findViewById(R.id.system_paper);
	}

	private void initViews() {
		String wallpaper = AppPreference.getLockScreenWallpaper(this);
		if (wallpaper.equals("default")) {
			mWallpaper.setImageResource(AppPreference.getDefaultWallpaperImg(this));
		} else if (wallpaper.equals("system")) {
			mWallpaper.setImageDrawable(BitmapUtil.getWallPaper(LockWallpaperSelectActivity.this, false));
		} else {
			Uri imageUri = Uri.parse(wallpaper);
			mWallpaper.setImageURI(imageUri);
		}

		mDefaultTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivityForResult(new Intent(LockWallpaperSelectActivity.this, DefaultWallpaperSelectActivity.class), TAKE_DEFAULT_PICTURE);
			}
		});
		mMyTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String strAvatarPrompt = "选择壁纸";
				Intent pickPhoto = new Intent(Intent.ACTION_PICK);
				pickPhoto.setType("image/*");
				startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
			}
		});
		mSystemTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AppPreference.setLockScreenWallpaper(LockWallpaperSelectActivity.this, "system");
				mWallpaper.setImageDrawable(BitmapUtil.getWallPaper(LockWallpaperSelectActivity.this, false));
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

}
